package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    public CategoryService categoryService;

    @Autowired
    public DishService dishService;

    @Autowired
    public SetmealService setmealService;

    @GetMapping("/page")
    public R<Page> getpage(Integer page, Integer pageSize) {

        Page<Category> page1 = new Page<>(page, pageSize);

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();

        LambdaQueryWrapper<Category> wrapper1 = wrapper.orderByDesc(Category::getUpdateTime);

        Page<Category> page2 = categoryService.page(page1, wrapper1);

        return R.success(page2);
    }

    @PostMapping
    public R<String> add(@RequestBody Category category) {



            categoryService.save(category);

        return R.success("添加成功");

    }

    @DeleteMapping
    public R<String> delete(Long id) {
        categoryService.remove(id);
        return R.success("删除成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){

        categoryService.updateById(category);

        return R.success("修改成功");
    }

//    @GetMapping("/list")
//    public R<List> getlist(Integer type){
//
//        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
//
//        wrapper.eq(Category::getType,type);
//
//        List<Category> list = categoryService.list(wrapper);
//
//        return R.success(list);
//    }
    @GetMapping("/list")
    public R<List<Category>> list(Integer type){

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type!=null,Category::getType,type);
        List<Category> list = categoryService.list(wrapper);

        return R.success(list);

    }
}
